/**    
* @Title: RoleServiceImpl.java  
* @Package com.kaver.sso.service.impl   
* @author roc   
* @date Aug 9, 2021 11:22:57 AM  
* @version V1.0  
* @copyright kaver 
*/
package com.kaver.sso.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaver.sso.dao.po.SsoRole;
import com.kaver.sso.dao.po.SsoUserRole;
import com.kaver.sso.dao.service.SsoRoleDao;
import com.kaver.sso.dao.service.SsoUserRoleDao;
import com.kaver.sso.def.BizException;
import com.kaver.sso.def.dto.RoleDto;
import com.kaver.sso.def.enums.CodeEnum;
import com.kaver.sso.def.req.AddRoleReq;
import com.kaver.sso.def.req.DelRoleReq;
import com.kaver.sso.def.req.RoleListReq;
import com.kaver.sso.def.req.RolePageListReq;
import com.kaver.sso.def.req.SetRoleReq;
import com.kaver.sso.def.req.UpdateRoleReq;
import com.kaver.sso.def.resp.PageResp;
import com.kaver.sso.service.RoleService;

/**  
* @ClassName: RoleServiceImpl  
* @Description: 角色
* @author roc  
* @date Aug 9, 2021 11:22:57 AM  
*    
*/
@Service
public class RoleServiceImpl implements RoleService{
    @Autowired
    SsoRoleDao ssoRoleDao;
    @Autowired
    SsoUserRoleDao ssoUserRoleDao;
    @Override
    public void addRole(AddRoleReq req) {
        SsoRole entity = new SsoRole();
        entity.setGroupId(req.getGroupId());
        entity.setRoleName(req.getRoleName());
        ssoRoleDao.save(entity);
    }

    @Override
    public void delRole(DelRoleReq req) {
        SsoRole role = ssoRoleDao.getById(req.getRoleId());
        if (role == null) {
            throw new BizException(CodeEnum.F_000010);
        }
        int count = ssoUserRoleDao.queryRoleUserCount(req.getRoleId());
        if (count > 0) {
        	throw new BizException(CodeEnum.F_000001.getCode(),"当前角色存在用户，请先清除用户角色");
        }
        role.deleteById();
    }

    @Override
    public void updateRole(UpdateRoleReq req) {
        SsoRole role = ssoRoleDao.getById(req.getRoleId());
        if (role == null) {
            throw new BizException(CodeEnum.F_000010);
        }
        if (req.getRoleName() != null) {
            role.setRoleName(req.getRoleName());
        }
        if (req.getGroupId() != null) {
            role.setGroupId(req.getGroupId());
        }
        role.updateById();
    }

    @Override
    public PageResp<RoleDto> rolePageList(RolePageListReq req) {
        SsoRole query = new SsoRole();
        query.setGroupId(req.getGroupId());
        QueryWrapper<SsoRole> queryWrapper = new QueryWrapper<SsoRole>(query);
        if (req.getRoleName() != null) {
            queryWrapper.like("role_name", req.getRoleName());
        }
        queryWrapper.orderByDesc("id");
        IPage<SsoRole> page = new Page<>(req.getPage(), req.getSize());
        IPage<SsoRole> iPage = ssoRoleDao.page(page, queryWrapper);
        PageResp<RoleDto> pageResp = new PageResp<>();
        List<SsoRole> roles =  iPage.getRecords();
        List<RoleDto> lists = new ArrayList<>();
        if (!CollectionUtils.isEmpty(roles)) {
            for (SsoRole role : roles) {
                RoleDto dto = new RoleDto();
                dto.setGroupId(role.getGroupId());
                dto.setRoleId(role.getId());
                dto.setRoleName(role.getRoleName());
                lists.add(dto);
            }
        }
        pageResp.setLists(lists);
        pageResp.setTotal(iPage.getTotal());
        return pageResp;
    }

    @Override
    public List<RoleDto> roleList(RoleListReq req) {
        List<RoleDto> lists = new ArrayList<>();
        SsoRole query = new SsoRole();
        query.setGroupId(req.getGroupId());
        QueryWrapper<SsoRole> queryWrapper = new QueryWrapper<SsoRole>(query);
        if (req.getRoleName() != null) {
            queryWrapper.like("role_name", req.getRoleName());
        }
        if (!CollectionUtils.isEmpty(req.getGroupIds())) {
            queryWrapper.in("group_id", req.getGroupIds());
        }
        queryWrapper.orderByDesc("id");
        List<SsoRole> roles = ssoRoleDao.list(queryWrapper);
        if (!CollectionUtils.isEmpty(roles)) {
            for (SsoRole role : roles) {
                RoleDto dto = new RoleDto();
                dto.setGroupId(role.getGroupId());
                dto.setRoleId(role.getId());
                dto.setRoleName(role.getRoleName());
                lists.add(dto);
            }
        }
        return lists;
    }

    @Override
    public List<RoleDto> queryUserRoles(String userId) {
        SsoUserRole query = new SsoUserRole();
        query.setUserId(userId);
        QueryWrapper<SsoUserRole> queryWrapper = new QueryWrapper<SsoUserRole>(query);
        List<SsoUserRole> list = ssoUserRoleDao.list(queryWrapper);
        List<RoleDto> dtos = new ArrayList<>();
        for (SsoUserRole role : list) {
            RoleDto dto = new RoleDto();
            SsoRole ssoRole = ssoRoleDao.getById(role.getRoleId());
            dto.setGroupId(ssoRole.getGroupId());
            dto.setRoleId(ssoRole.getId());
            dto.setRoleName(ssoRole.getRoleName());
            dtos.add(dto);
        }
        return dtos;
    }

    @Override
    public void setRole(SetRoleReq req) {
        SsoUserRole query = new SsoUserRole();
        query.setUserId(req.getUserId());
        QueryWrapper<SsoUserRole> queryWrapper = new QueryWrapper<SsoUserRole>(query);
        ssoUserRoleDao.remove(queryWrapper);
        List<SsoUserRole> entityList = new ArrayList<>();
        List<Long> roleIds = req.getRoleIds();
        for (Long roleId : roleIds) {
            SsoUserRole userRole = new SsoUserRole();
            userRole.setRoleId(roleId);
            userRole.setUserId(req.getUserId());
            entityList.add(userRole);
        }
        ssoUserRoleDao.saveBatch(entityList);
    }

}
